package com.ccjq.shosestore.web.controller.member;

import com.ccjq.shosestore.core.domain.PageInfo;
import com.ccjq.shosestore.core.utils.PageUtils;
import com.ccjq.shosestore.member.bo.User;
import com.ccjq.shosestore.order.bo.OrderBo;
import com.ccjq.shosestore.order.service.OrderService;
import com.ccjq.shosestore.order.service.impl.OrderServiceImpl;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

/**
 * 以下常量皆为标识参数，servlet根据该参数执行具体动作
 *
 * @value serialVersionUID 序列化ID
 * @value QUERY_ORDER_INFO 查询订单简要信息，支持分页
 * @value QUERY_ORDER_ALL 查询单条订单详细信息
 */
public class OrderServlet extends HttpServlet {
    private static final long serialVersionUID = 4375466830388301092L;
    private static final String QUERY_ORDER_INFO = "qoi";
    private static final String QUERY_ORDER_ALL = "qo";

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String flag = req.getParameter("flag");
        System.out.println("flag = " + flag);
        if (StringUtils.isBlank(flag)) {
            toOrderPage(req, resp);
        } else if (QUERY_ORDER_INFO.equals(flag)) {
            toOrderPage(req, resp);//跳转到我的订单
        }else if(QUERY_ORDER_ALL.equals(flag)){
            toOrderPages(req,resp);
        }
    }
    OrderService orderService = new OrderServiceImpl();

    private void toOrderPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


        //判断用户是否登陆
            HttpSession session = request.getSession();
            Object tempUser = session.getAttribute("user");
            if (tempUser == null) {
            RequestDispatcher requestDispatcher = request.getRequestDispatcher(request.getContextPath() + "/WEB-INF/jsp/member/login.jsp");
            requestDispatcher.forward(request, response);
        }
        String type = request.getParameter("flag");



        System.out.println("前台获取的类型：" + type);
        System.out.println("系统常量：" + QUERY_ORDER_INFO);
        System.out.println("判断的值" + QUERY_ORDER_INFO.equals(type));
        System.out.println("准备分页查询：");

        //查询分页订单动作
        User user = (User) tempUser;
        System.out.println("获取用户：" + (user != null));
        String userId = user.getUserid();
        System.out.println("session获取的用户id" + userId);



        //查询页数
        String pageNo = request.getParameter("pageNo");
        String pageSize = request.getParameter("pageSize");

        if(StringUtils.isBlank(pageNo)){
            pageNo = "1";
        }
        if(StringUtils.isBlank(pageSize)){
            pageSize = "4";
        }


        //获取总行数，用于PageBean的参数分发
        int totalRows = ((OrderServiceImpl) orderService).getTotalRows(userId);
        //总页数
        int totalPage = PageUtils.getPageCount(totalRows, Integer.parseInt(pageSize));

        //起始行数
        int startRow = PageUtils.getStartRow(Integer.parseInt(pageSize), Integer.parseInt(pageNo));

        System.out.println("总页数" + totalPage);
        System.out.println("当前页数" + pageNo);
        System.out.println("每页大小" + pageSize);
        List<OrderBo> list = orderService.queryOrdersByUserId(userId, startRow);


        PageInfo pageinfo=new PageInfo();
        pageinfo.setObjs(list);
        pageinfo.setPageNo(Integer.parseInt(pageNo));
        pageinfo.setPages(totalPage);
        pageinfo.setPageSize(Integer.parseInt(pageSize));
        pageinfo.setTotal(totalRows);
        request.setAttribute("pageInfo", pageinfo);


        RequestDispatcher rd = request.getRequestDispatcher(request.getContextPath() + "/WEB-INF/jsp/order/showOrderAll.jsp");
        rd.forward(request, response);

    }
    private void toOrderPages(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //查询订单详细信息
        String orderId = request.getParameter( "orderId" );
        System.out.println("订单编号"+orderId);
        OrderBo orderBo = orderService.queryOrdersByOrderId( orderId );
        request.setAttribute( "orderBo", orderBo );
        RequestDispatcher rd = request.getRequestDispatcher( request.getContextPath() + "/WEB-INF/jsp/order/detailedOrder.jsp" );
        rd.forward( request, response );
    }

}
